/**
 * 
 */
package org.swing.utility.jai.file;

import java.io.File;

/**
 * @author Quynh Nhu
 *
 */
public class Path 
{
    /**
     * Returns the current operating system's path separator character as a string.
     */
    public static String pathSeparator = System.getProperty("file.separator");
    
    /**
     * Validates path.
     * @param path Path to validate.
     * @return True if path exists, otherwise false.
     */
    public static boolean ValidatePath(String path)
    {
        if(!new java.io.File(path).exists())
            return false;
        else
            return true;
    }
    
    /**
     * FixPath appends trailing slash to the path.
     * @param path Path to fix.
     * @return Path with trailing slash appended.
     */
    public static String FixPath(String path)
    {
        String sep = System.getProperty("file.separator");
        
        if(path.lastIndexOf(sep) != path.length()-1)
            path += sep;
        
        return path;
    }
    
    /**
     * Delete files and folder.
     * @param path Path to directory to delete
     * @return
     */
    public static boolean deleteDir(File dir) 
    {
        if(dir.exists())
        {
            if (dir.isDirectory()) 
            {
                String[] children = dir.list();
                for (int i=0; i<children.length; i++) 
                {
                    boolean success = deleteDir(new File(dir, children[i]));

                    if (!success) 
                    {
                        return false;
                    }
                }
            }

            // The directory is now empty so delete it
            return dir.delete();
        }
        
        return false;
    } 
    
    public static boolean createPath(String path)
    {
        boolean ok = true;
        
        if(!ValidatePath(path))
            ok = (new java.io.File(path)).mkdir();
        
        return ok;
    }

    /**
     * Get filename without extension
     * @param fileName
     * @return String containing file path without extension.
     */
    public static String getFileNameWithoutExtension(String fileName)
    {
        File tmpFile = new File(fileName);
        tmpFile.getName();

        int whereDot = tmpFile.getName().lastIndexOf('.');

        if (0 < whereDot && whereDot <= tmpFile.getName().length() - 2 ) 
        {
            return tmpFile.getName().substring(0, whereDot);
        }

        return "";
    }
    
    /**
     * Get filename without path
     * @param fileName
     * @return String containing filename.
     */
    public static String getFileName(String fileName)
    {
        File tmpFile = new File(fileName);

        if(tmpFile.exists())
        {
            return tmpFile.getName();
        }
        else
            return "";
    }

    /**
     * Get the current application path.
     *
     * @return String containing path.
     */
    public static String getApplicationPath()
    {
        File currentDir = new java.io.File("");
        String dir = currentDir.getAbsolutePath();

        return dir;
    }

    /**
     * Get path portion of fileName.
     *
     * @param fileName
     *
     * @return Path with trailing slash.
     */
    public static String getPath(String fileName)
    {
        File tmpFile = new File(fileName);

        return FixPath(tmpFile.getParent());
    }
}
